Matplotlib Tutorial Part 02: Legends titles and labels

Source


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt

Data Generations Function


In [2]:
def generate_data(x=10,ran=9):
    raw = (randint(0,ran) for _ in range(x))
    return zip(*enumerate(raw))

One Line Graph


In [3]:
fig = plt.figure(figsize=(17,9))
X = [1,2,3]
Y = [5,6,1]

X, Y = generate_data(ran=5)
plt.plot(X, Y, label='Graph')

plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')
plt.title('Function\nOr Graph')

# Plotting
plt.grid(True)
plt.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-fb705c3a7f73> in <module>()
      3 Y = [5,6,1]
      4 
----> 5 X, Y = generate_data(ran=5)
      6 plt.plot(X, Y, label='Graph')
      7 

<ipython-input-2-bd8dc118fcdf> in generate_data(x, ran)
      1 def generate_data(x=10,ran=9):
      2     raw = (randint(0,ran) for _ in range(x))
----> 3     return zip(*enumerate(raw))

<ipython-input-2-bd8dc118fcdf> in <genexpr>(.0)
      1 def generate_data(x=10,ran=9):
----> 2     raw = (randint(0,ran) for _ in range(x))
      3     return zip(*enumerate(raw))

NameError: name 'randint' is not defined
<matplotlib.figure.Figure at 0x13ce8ec28d0>

Several Line Graph on the same box


In [1]:
from random import randint

fig = plt.figure(figsize=(17,9))


X, Y = generate_data(ran=5)
plt.plot(X, Y, label='Graph1')
X, Y = generate_data(ran=20)
plt.plot(X, Y, label='Graph2')
X, Y = generate_data()
plt.plot(X, Y, label='Graph3')

# Labels
plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')
plt.title('Function\nOr Graph')
plt.legend()

# Plotting
plt.grid(True)
plt.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-9fb41f6e30f5> in <module>()
      1 from random import randint
      2 
----> 3 fig = plt.figure(figsize=(17,9))
      4 
      5 

NameError: name 'plt' is not defined